﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DCS.ComLib.Config;
using VietThuongLMS.Services;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;
using System.Web.Security;
using VietThuongLMS.Areas.Admin.Models;

///Author: NHDuc
namespace VietThuongLMS.Areas.Admin.Controllers
{
    public class AccountController : Controller
    {


        //[Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            if (!Request.IsAuthenticated)
                return RedirectToAction("Login", "Account", new { area = "Admin" });

            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            AccountViewModel accountList = AccountService.GetAccountList(int.Parse(ConfigReader.ReadString("ItemPerPage")), page);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "Account", "Index", "?p=", accountList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(accountList);
        }

        //[Authorize(Roles = "Admin")]
        public ActionResult Detail(string id)
        {
            TaiKhoan Account = AccountService.GetAccountByName(id);
            return View(Account);
        }

        //[Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            using (LMSDataEntities context = new LMSDataEntities())
            {
                ViewData["CBRole"] = new SelectList(context.LoaiTaiKhoans.ToList(), "ID", "TenLoaiTaiKhoan");
                return View();
            }

        }

        //[Authorize(Roles = "Admin")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(AccountModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    TaiKhoan data = new TaiKhoan();
                    //data.userName = model.UserName;
                    //data.passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(ConfigReader.ReadString("DefaultPassword"), "md5");
                    //data.fullName = model.FullName;
                    //data.address = model.Address;
                    //data.phone = model.Phone;
                    //data.email = model.Email;
                    //data.roleId = model.Role;
                    //data.signupDate = DateTime.Now;
                    bool result = AccountService.InsertAccount(data);
                    if (result)
                        return RedirectToAction("Index", "Account", new { area = "Admin" });
                    using (LMSDataEntities context = new LMSDataEntities())
                    {
                        ViewData["CBRole"] = new SelectList(context.LoaiTaiKhoans.ToList(), "MaLoaiTaiKhoan", "TenLoaiTaiKhoan");
                        return View(model);
                    }
                }
                catch (Exception ex)
                {

                }
            }
            return View(model);
        }

        //delete Account
        //[Authorize(Roles = "Admin")]
        [HttpGet]
        public ActionResult Delete(string id)
        {
            bool result = AccountService.DeleteAccount(id);
            if (result)
            {
                return RedirectToAction("Index", "Account", new { area = "Admin" });
            }
            else
            {
                return View();
            }
            
        }

        //[Authorize(Roles = "Admin")]
        public ActionResult Edit(string id)
        {
            TaiKhoan account = AccountService.GetAccountByName(id);
            //AccountModel model = new AccountModel();
            //model.UserName = account.userName;
            //model.FullName = account.fullName;
            //model.CreatedDate = account.signupDate.Value.ToString();
            string test = account.MaLoaiTaiKhoan;
            using (LMSDataEntities context = new LMSDataEntities())
            {
                ViewData["CBRole"] = new SelectList(context.LoaiTaiKhoans.ToList(), "id", "name", test);
            }
            return View(account);
        }

        //[Authorize(Roles = "Admin")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(TaiKhoan comp)
        {
            AccountService.Edit(comp);
            return RedirectToAction("Index", "Account", new { area = "Admin" });
        }

        //[HttpPost]
        //public bool CheckPass(string oldpass)
        //{
        //    return AccountService.SignIn(HttpContext.User.Identity.Name, FormsAuthentication.HashPasswordForStoringInConfigFile(oldpass, "md5"));
        //}

        [Authorize]
        public ActionResult ChangePass()
        {
            return View();
        }

        [Authorize]
        [HttpPost, ValidateInput(false)]
        public ActionResult ChangePassSubmit(ChangePassModel comp)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    TaiKhoan data = new TaiKhoan();
                    //data.userName = HttpContext.User.Identity.Name;
                    //data.passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(comp.OldPass, "md5");
                    string newPass = FormsAuthentication.HashPasswordForStoringInConfigFile(comp.NewPass, "md5");
                    bool result = AccountService.ChangePass(data, newPass);
                    if (result)
                        return RedirectToAction("Index", "Account", new { area = "Admin" });
                    else
                    {
                        ModelState.AddModelError("", "Wrong password");
                        return RedirectToAction("ChangePass", "Account", new { area = "Admin" });
                    }
                }
                catch (Exception ex)
                {

                }
            }
            return View("Create");
        }

        [Authorize]
        [HttpPost]
        public string ResetPass(string userid)
        {
            try
            {
                TaiKhoan data = new TaiKhoan();
                data.MaTaiKhoan = userid;
                data.MatKhau = FormsAuthentication.HashPasswordForStoringInConfigFile("123456", "md5");
                bool result = AccountService.ResetPass(data);
                if (result)
                    return "1";
                else
                {
                    return "0";
                }
            }
            catch (Exception ex)
            {
                return "0";
            }
        }

        [HttpPost]
        public bool CheckValid(string username)
        {
            if (AccountService.GetAccount(username) != null)
                return false;
            return true;
        }

    }
}
